//
//  PlayersViewController.m
//  Ratings
//
//  Created by steven.zhan on 14-9-15.
//  Copyright (c) 2014年 hpxbzz. All rights reserved.
//

#import "PlayersViewController.h"
#import "Player.h"
#import "PlayCell.h"

@interface PlayersViewController ()

@end

@implementation PlayersViewController

@synthesize players;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.players count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PlayerCell";
    Player *player = [self.players objectAtIndex:indexPath.row];
    
    PlayCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
//    UILabel *nameLabel=(UILabel*)[cell viewWithTag:100];
//    nameLabel.text=player.name;
//    
//    UILabel *gameLabel=(UILabel*)[cell viewWithTag:101];
//    gameLabel.text=player.game;
//    
//    UIImageView *ratingImageViwe = (UIImageView*)[cell viewWithTag:102];
//    ratingImageViwe.image=[self imageForRating:player.rating];
    // Configure the cell...
    cell.nameLabel.text=player.name;
    cell.gameLabel.text=player.game;
    cell.ratingImageView.image=[self imageForRating:player.rating];
    return cell;
}

- (UIImage*)imageForRating:(int)rating
{
    switch (rating) {
        case 1:
            return [UIImage imageNamed:@"1StarSmall.png"];
        case 2:
            return [UIImage imageNamed:@"2StarsSmall.png"];
        case 3:
            return [UIImage imageNamed:@"3StarsSmall.png"];
        case 4:
            return [UIImage imageNamed:@"4StarsSmall.png"];
        case 5:
            return [UIImage imageNamed:@"5StarsSmall.png"];
    }
    return NULL;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [self.players removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"EditPlayer" sender:indexPath];
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

 */

#pragma mark - PlayerDetailsViewControllerDelegate

- (void)playerDetailsViewControllerDidCancel:(PlayerDetailsViewController*)controller
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)playerDetailsViewControllerDidSave:(PlayerDetailsViewController*)controller didAddPlayer:(Player *)player
{
    [self.players addObject:player];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.players.count-1 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)playerDetailsViewControllerDidEdit:(PlayerDetailsViewController*)controller didEditPlayer:(Player*)player
{
    NSUInteger index = [self.players indexOfObject:players];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"AddPlayer"]) {
        UINavigationController *navigationController=segue.destinationViewController;
        PlayerDetailsViewController *playDetailsViewController = [[navigationController viewControllers] objectAtIndex:0];
        playDetailsViewController.delegate=self;
    }
    else if([segue.identifier isEqualToString:@"EditPlayer"]){
        UINavigationController *navigationController=segue.destinationViewController;
        PlayerDetailsViewController *playDetailsViewController = [[navigationController viewControllers] objectAtIndex:0];
        playDetailsViewController.delegate=self;
        
        NSIndexPath *indexPath=sender;
        Player *player = [self.players objectAtIndex:indexPath.row];
        playDetailsViewController.playerToEdit=player;
    }
    else if([segue.identifier isEqualToString:@"RatePlayer"])
    {
        RatePlayerViewController *ratePlayerViewController = segue.destinationViewController;
        ratePlayerViewController.delegate = self;
        
        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
        Player *player = [self.players objectAtIndex:indexPath.row];
        ratePlayerViewController.player = player;
    }
}

- (void)setPlayers:(NSMutableArray *)p
{
    if (players != p)
        players=p;
    [self.tableView reloadData];
}

#pragma mark - RatePlayerViewControllerDelegate
- (void)ratePlayerViewController:(RatePlayerViewController*)controller
          didPickRatingForPlayer:(Player*)player
{
    NSUInteger index = [self.players indexOfObject:players];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
